package models

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"gofly/common"
	"log"
)

func MysqlConnect() error {
	mysql := common.NewMysqlConfig()
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Database)
	var err error
	gormDB, err = gorm.Open("mysql", dsn)
	if err != nil {
		log.Println(err)
		return err
	}
	gormDB.SingularTable(true)
	gormDB.LogMode(true)
	gormDB.DB().SetMaxIdleConns(10)
	gormDB.DB().SetMaxOpenConns(100)
	return nil
}
func MysqlExecute(sql string) error {
	return gormDB.Exec(sql).Error
}
func CloseDB() {
	defer gormDB.Close()
}
